define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activityone/useronlist/index' + location.search,
                    add_url: 'activityone/useronlist/add',
                    del_url: 'activityone/useronlist/del',
                    multi_url: 'activityone/useronlist/multi',
                    import_url: 'activityone/useronlist/import',
                    table: 'event_checkin_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                pageSize: 300,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user.nickname', title: __('User_id') },
                        { field: 'date', title: __('Date') },
                        // {field: 'checkin_type', title: __('Checkin_type'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        { field: 'status', title: __('Status'), searchList: { "1": __('Status 1'), "2": __('Status 2') }, formatter: Table.api.formatter.status },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'videoimage', title: __('Videoimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'events.name', title: __('Event_id') },
                        { field: 'checkin.name', title: __('Checkin_id') },
                        { field: 'created_at', title: __('Created_at') },
                        { field: 'number_total', title: __('Numb'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看用户打卡记录'),
                                    title: __('查看用户打卡记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=["99%","99%"]',
                                    url: 'activityone/useronlisttwo?eventid={event_id}&userid={user_id}',

                                },


                            ],
                        }
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    if (Config.event_id) {
                        filter.checkin_id = Config.event_id;
                        op.checkin_id = "=";
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);
                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value == 1) {
                        //这里可以自定义多个筛选条件
                        filter.is_submit = 1;
                        op.is_submit = '=';

                        filter.is_coupon = 1;
                        op.is_coupon = "<>";
                    } else {
                        //选全部时要移除相应的条件
                        delete filter.is_submit;
                        delete op.is_submit;
                        delete filter.is_coupon;
                        delete op.is_coupon;
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', { pageNumber: 1 });
                return false;
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
